<?php


namespace App\Logic;

use App\Models\Fang;
use App\Models\Tags;
use App\server\EsServer;
use App\server\RedisServer;

/**
 * @name wjy
 * Class LabeLLogic
 * @package App\Logic
 */
class LabeLLogic
{
    /**
     * 展示房源标签列表
     */
    public static function Secondary(){
        $redis=RedisServer::createRedisObject();
        $redis->select(14);
        $size=['data'=>$redis->getAll()];
        return $size;
    }
    /**
     * 展示房源标列表同步数据库数据
     */
    public static function redisLabelList(){
        $redis=RedisServer::createRedisObject();
        $redis->select(14);
        $data= Tags::all()->toArray();
        foreach ($data as $key => $value){
            $key= array_keys($value);
            for ($i=0;$i<count($key);$i++){
                $redis->hset($value['id'],$key[$i],$value[$key[$i]]);
            }
        }
    }
    /**
     * 房源标签添加
     */
    public static function SecondaryAdd($request){
        $validated = $request->validate([
            'name' => 'required',
            'category' => 'required',
            'priority' => 'required',
        ],[
            'name.required'=>'不能为空',
            'category.required'=>'不能为空',
            'priority.required'=>'不能为空',
        ]);
        $SecondaryModel=Tags::create($validated);
        return $SecondaryModel;
    }
    /**
     * 房源标签删除
     */
    public static function SecondaryDel($fangId){
        if(empty($fangId))throw new \Exception('未接收到想要删除的房源标签信息');
        $executeDel=Tags::del('id',$fangId);
        return $executeDel;
    }
    /**
     * 房源标签查找
     */
    public static function SecondarySeek($query){
        if(empty($query)){
            return Tags::list();
        }
        $executeSeek=Tags::Seek('category',$query);
        return $executeSeek;
    }
    /**
     * 房源标签详情
     */
    public static function SecondaryDetails($fangID){
        if(empty($fangID))
        {
            throw new \Exception('未接收到想要查看的房源标签信息');
        }
        $executeDetail=Tags::detail('id',$fangID);
        return $executeDetail;
    }
    /**
     * 房源标签修改
     */
    public static function SecondaryUpdated($SecondaryMessageUpdated){
        unset($SecondaryMessageUpdated['updated_at']);
        unset($SecondaryMessageUpdated['created_at']);
        unset($SecondaryMessageUpdated['deleted_at']);
        $fangID=$SecondaryMessageUpdated['id'];
        $SecondaryModel=Tags::change('id',$fangID,$SecondaryMessageUpdated);
        return $SecondaryModel;
    }
    /**
     * 房源标签es搜索
     */
    public static function SecondarySearch($query){
        if(empty($query)){
            return Tags::list();
        }
        $executeEsSearch=new EsServer();
        $EsMessage['data']=$executeEsSearch->searchEs('fang',$query,'fang_name');
        return $EsMessage;
    }
}
